<template>
  <a-modal
    title="编辑BOM关联原料"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          style="display: none;"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input v-decorator="['bomMaterialId']"/>
        </a-form-item>

        <a-form-item
          label="主键"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入主键" v-decorator="['bomMaterialId', {rules: [{required: true, message: '请输入主键！'}]}]" />
        </a-form-item>

        <a-form-item
          label="BOM ID"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入BOM ID" v-decorator="['bomId', {rules: [{required: true, message: '请输入BOM ID！'}]}]" />
        </a-form-item>

        <a-form-item
          label="来源BOM ID"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入来源BOM ID" v-decorator="['bomIdSource', {rules: [{required: true, message: '请输入来源BOM ID！'}]}]" />
        </a-form-item>

        <a-form-item
          label="原料排序"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入原料排序" v-decorator="['bomMaterialSort', {rules: [{required: true, message: '请输入原料排序！'}]}]" />
        </a-form-item>

        <a-form-item
          label="原料ID"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入原料ID" v-decorator="['materialId', {rules: [{required: true, message: '请输入原料ID！'}]}]" />
        </a-form-item>

        <a-form-item
          label="原料数量"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入原料数量" v-decorator="['materialNumber', {rules: [{required: true, message: '请输入原料数量！'}]}]" />
        </a-form-item>

        <a-form-item
          label="创建时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入创建时间" v-decorator="['createTime', {rules: [{required: true, message: '请输入创建时间！'}]}]" />
        </a-form-item>

        <a-form-item
          label="创建人"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入创建人" v-decorator="['createUser', {rules: [{required: true, message: '请输入创建人！'}]}]" />
        </a-form-item>

        <a-form-item
          label="更新时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入更新时间" v-decorator="['updateTime', {rules: [{required: true, message: '请输入更新时间！'}]}]" />
        </a-form-item>

        <a-form-item
          label="更新人"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入更新人" v-decorator="['updateUser', {rules: [{required: true, message: '请输入更新人！'}]}]" />
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { edit } from '@/api/modular/main/emsbommaterial/emsBomMaterialManage'

  export default {
    data() {
      return {
        labelCol: {
          xs: {span: 24},
          sm: {span: 5}
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 15}
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      edit(record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue({
            bomMaterialId: record.bomMaterialId,
            bomId: record.bomId,
            bomIdSource: record.bomIdSource,
            bomMaterialSort: record.bomMaterialSort,
            materialId: record.materialId,
            materialNumber: record.materialNumber,
            createTime: record.createTime,
            createUser: record.createUser,
            updateTime: record.updateTime,
            updateUser: record.updateUser,
          });
        }, 100)
      },
      handleSubmit() {
        const {form: {validateFields}} = this
        this.confirmLoading = true

        validateFields((errors, values) => {
          if (!errors) {
            edit(values).then((res) => {
              this.confirmLoading = false
              if (res.success) {
                this.$message.success('编辑成功')
                this.handleCancel()
                this.$emit('ok', values)
              } else {
                this.$message.info('编辑失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel() {
        this.form.resetFields();
        this.visible = false
      }
    }
  }
</script>
